package com.android.ailaoshan.picture;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;

import com.android.ailaoshan.R;
import com.android.ailaoshan.common.BaseApplication;
import com.github.chrisbanes.photoview.PhotoView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * 查看图片大图，删除图片
 */
public class PlayActivity extends AppCompatActivity {

    private BaseApplication baseApplication;

    private final String PIC_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
            + File.separator +"AiLaoShan"+ File.separator + "Picture" + File.separator;

    private PhotoView photoView;
    private Toolbar toolbar;
    private TextView deleteText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.picture_play_activity);

        baseApplication = BaseApplication.getInstance();
        baseApplication.addActivity(PlayActivity.this);

        photoView = findViewById(R.id.picture_play_picture);
        toolbar = findViewById(R.id.picture_play_toolbar);
        deleteText = findViewById(R.id.picture_play_delete);
    }

    @Override
    protected void onResume(){
        super.onResume();

        String picPath = getIntent().getStringExtra("fileName");
        int saveType = getIntent().getIntExtra("saveType", 3);
        try {
            FileInputStream inputStream = new FileInputStream(PIC_PATH+picPath);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            photoView.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        if(saveType == 3){
            deleteText.setVisibility(View.INVISIBLE);
        }

        deleteText.setOnClickListener(
            (v) -> {
                UpdatePicture updatePicture = new UpdatePicture();
                updatePicture.deletePicture(picPath);

                // 设置返回数据
                Intent intent = new Intent();
                intent.putExtra("fileName",picPath);
                setResult(603,intent);
                baseApplication.removeActivity(PlayActivity.this);
            }
        );

        toolbar.setNavigationOnClickListener(
            (v) -> baseApplication.removeActivity(PlayActivity.this)
        );
    }
}
